package com.compass.doctor.ui.patient.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import com.compass.doctor.callback.OnCustomButtonClick
import com.compass.doctor.databinding.LayoutPrescribedItemBinding
import com.compass.framework.adapter.BaseBindViewHolder
import com.compass.framework.adapter.BaseRecyclerViewAdapter
import com.compass.framework.ext.gone
import com.compass.framework.ext.onClick
import com.compass.framework.ext.visible
import com.compass.framework.manager.DictionariesManager
import com.compass.framework.model.Diagnosis
import com.compass.framework.model.Drug
import com.compass.framework.model.TemplateDetails
import com.compass.framework.utils.DateUtils

/**
 * Description： 已开处方(历史处方，患者已开处方，已开处方列表)Adapter
 * Date:2023/12/6 15:09
 */
class PrescribedAdapter : BaseRecyclerViewAdapter<TemplateDetails, LayoutPrescribedItemBinding>() {


    private  var onCustomBottomClick: OnCustomButtonClick?=null
    fun setBottomCallBack(onChatBottomClick: OnCustomButtonClick) {
        this.onCustomBottomClick = onChatBottomClick
    }

    override fun onBindDefViewHolder(
        holder: BaseBindViewHolder<LayoutPrescribedItemBinding>,
        item: TemplateDetails?,
        position: Int
    ) {
        if (item == null) return
        holder.binding.apply {
            tvTime.text = DateUtils.conversionTime(item.createTime, DateUtils.FORMAT_TWO)
            tvPatientName.text = item.patientName
            tvPaymentStatus.text = DictionariesManager.getOrderStatus(item.status)
            tvDiagnosis.text = getDiagnosis(item.diagnosis)
            tvDrugs.text = getDrugs(item.drugs)
            when(item.status){
                "0","1"->{
                    tvCancellation.visible()
                    llReason.gone()
                }
                "-1"->{
                    //作废
                    tvCancellation.gone()
                    llReason.visible()
                    tvReasonName.text="作废原因"
                    tvReason.text=item.cancelDescription
                }
                "-2"->{
                    //拒绝
                    tvCancellation.gone()
                    tvReasonName.text="拒绝原因"
                    llReason.visible()
                    tvReason.text=item.cancelDescription
                }
                else->{
                    llReason.gone()
                    tvCancellation.gone()
                }
            }


            tvCancellation.onClick {
                onCustomBottomClick?.onButtonCallBack(position, "cancellation")
            }

            tvDetails.onClick {
                onCustomBottomClick?.onButtonCallBack(position, "details")

            }
        }
    }

    override fun getViewBinding(
        layoutInflater: LayoutInflater,
        parent: ViewGroup,
        viewType: Int
    ): LayoutPrescribedItemBinding {
        return LayoutPrescribedItemBinding.inflate(layoutInflater, parent, false)
    }

    private fun getDiagnosis(list: MutableList<Diagnosis>?): String {
        list?.let {
            for (item in list) {
                if (item.main) return item.name
            }
        }
        return ""
    }

    private fun getDrugs(list: MutableList<Drug>?): String {
        var listName = ArrayList<String?>()
        list?.let {
            for (item in list) {
                listName.add(item.name)
            }
        }
        return listName.joinToString(separator = "/")
    }
}